package com.xunqi.mall.controller;

import com.xunqi.mall.consts.MallConst;
import com.xunqi.mall.form.CartAddForm;
import com.xunqi.mall.form.CartUpdateForm;
import com.xunqi.mall.service.CartService;
import com.xunqi.mall.vo.CartVo;
import com.xunqi.mall.vo.ResponseVo;
import com.xunqi.mall.vo.UserLoginVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Description:
 * @Created with IntelliJ IDEA.
 * @author: 夏沫止水
 * @create: 2020-04-24 11:54
 **/
@RestController
public class CartController {

    @Resource
    private CartService cartService;

    @GetMapping(value = "/carts")
    public ResponseVo<CartVo> cartList(HttpServletRequest request) {

        //获取当前用户登录信息
        UserLoginVo user = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo<CartVo> cartVoResponseVo = cartService.cartList(user.getId());

        return cartVoResponseVo;
    }


    @PostMapping(value = "/carts")
    public ResponseVo<CartVo> addCart(@Valid @RequestBody CartAddForm form,
                                      HttpServletRequest request) {

        //获取当前用户登录信息
        UserLoginVo user = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo<CartVo> cartVoResponseVo = cartService.addCart(user.getId(), form);

        return cartVoResponseVo;

    }


    @PutMapping(value = "/carts/{productId}")
    public ResponseVo<CartVo> updateCart(@PathVariable("productId") Integer productId,
                                         @Valid @RequestBody CartUpdateForm form,
                                         HttpServletRequest request) {

        //获取当前用户登录信息
        UserLoginVo user = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo<CartVo> cartVoResponseVo = cartService.updateCart(user.getId(),productId,form);

        return cartVoResponseVo;

    }

    @DeleteMapping(value = "/carts/{productId}")
    public ResponseVo<CartVo> deleteCart(@PathVariable("productId") Integer productId,
                                         HttpServletRequest request) {

        //获取当前用户登录信息
        UserLoginVo user = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo<CartVo> cartVoResponseVo = cartService.deleteCart(user.getId(),productId);

        return cartVoResponseVo;

    }


    @PutMapping(value = "/selectAll")
    public ResponseVo<CartVo> selectAllCart(HttpServletRequest request) {

        //获取当前用户登录信息
        UserLoginVo user = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo<CartVo> cartVoResponseVo = cartService.selectAll(user.getId());

        return cartVoResponseVo;
    }


    @PutMapping(value = "/unSelectAll")
    public ResponseVo<CartVo> unSelectAll(HttpServletRequest request) {

        //获取当前用户登录信息
        UserLoginVo user = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo<CartVo> cartVoResponseVo = cartService.unSelectAll(user.getId());

        return cartVoResponseVo;
    }


    @GetMapping(value = "/carts/products/sum")
    public ResponseVo sumCart(HttpServletRequest request) {
        //获取当前用户登录信息
        UserLoginVo user = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo<CartVo> cartVoResponseVo = cartService.sumCart(user.getId());

        return cartVoResponseVo;
    }


}
